package com.mifengkong.kotlincommon.recyclerview.wrap

import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.SparseArray
import android.view.View
import android.view.ViewGroup

/**
 * Created by jack on 2018/6/11
 */
class WrapRecyclerAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder> {

    // 列表的Adapter
    private var mAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>

    private var mHeaderViews: SparseArray<View>   //存放所有头部
    private var mFooterViews: SparseArray<View>   //存放所有尾部

    // 基本的头部类型开始位置  用于viewType
    private var BASE_ITEM_TYPE_HEADER = 10000000
    // 基本的底部类型开始位置  用于viewType
    private var BASE_ITEM_TYPE_FOOTER = 20000000

    constructor(adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>) {
        this.mAdapter = adapter
        mHeaderViews = SparseArray()
        mFooterViews = SparseArray()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (isHeaderViewType(viewType)) {
            val headerView = mHeaderViews.get(viewType)
            return createHeaderFooterViewHolder(headerView)
        } else if (isFooterViewType(viewType)) {
            val footerView = mFooterViews.get(viewType)
            return createHeaderFooterViewHolder(footerView)
        }
        return mAdapter.onCreateViewHolder(parent, viewType)
    }

    private fun isFooterViewType(viewType: Int): Boolean {
        return mFooterViews.indexOfKey(viewType) >= 0
    }

    private fun createHeaderFooterViewHolder(view: View): RecyclerView.ViewHolder {
        return object : RecyclerView.ViewHolder(view) {}
    }

    private fun isHeaderViewType(viewType: Int): Boolean {
        return mHeaderViews.indexOfKey(viewType) >= 0
    }

    override fun getItemCount(): Int {
        return mAdapter.itemCount + mHeaderViews.size() + mFooterViews.size()
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (isHeaderPosition(position) || isFooterPosition(position)) {
            return
        }
        val realPosition = position - mHeaderViews.size()
        mAdapter.onBindViewHolder(holder, realPosition)
    }

    override fun getItemViewType(position: Int): Int {
        var realPosition = position
        if (isHeaderPosition(realPosition)) {
            return mHeaderViews.keyAt(position)
        }
        if (isFooterPosition(position)) {
            realPosition = position - mHeaderViews.size() - mAdapter.itemCount
            return mFooterViews.keyAt(realPosition)
        }
        realPosition = position - mHeaderViews.size()
        return mAdapter.getItemViewType(realPosition)
    }

    fun isHeaderPosition(position: Int): Boolean {
        return position < mHeaderViews.size()
    }

    fun isFooterPosition(position: Int): Boolean {
        return position >= mAdapter.itemCount + mHeaderViews.size()
    }

    fun addHeaderView(view: View) {
        val position = mHeaderViews.indexOfValue(view)
        if (position < 0) {   //表示不在集合中
            mHeaderViews.put(BASE_ITEM_TYPE_HEADER++, view)
        }
        notifyDataSetChanged()
    }

    fun removeHeaderView(view: View) {
        val position = mHeaderViews.indexOfValue(view)
        if (position >= 0) {
            mHeaderViews.removeAt(position)
            notifyDataSetChanged()
        }
    }

    fun addFooterView(view: View) {
        val position = mFooterViews.indexOfValue(view)
        if (position < 0) {   //表示不在集合中
            mFooterViews.put(BASE_ITEM_TYPE_FOOTER++, view)
        }
        notifyDataSetChanged()
    }

    fun removeFooterView(view: View) {
        val position = mFooterViews.indexOfValue(view)
        if (position >= 0) {
            mFooterViews.removeAt(position)
            notifyDataSetChanged()
        }
    }

    /**
     * 解决GridLayoutManager添加头部和底部不占用一行的问题
     */
    fun adjustSpanSize(recycler: RecyclerView) {
        if (recycler.layoutManager is GridLayoutManager) {
            val layoutManager = recycler.layoutManager as GridLayoutManager
            layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                override fun getSpanSize(position: Int): Int {
                    val isHeaderOrFooter = isHeaderPosition(position) || isFooterPosition(position)
                    return if (isHeaderOrFooter) layoutManager.spanCount else 1
                }
            }
        }
    }

}